package com.shkco.xbjcc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shkco.exception.GlobalExceptionCode;
import com.shkco.utils.AssertUtil;
import com.shkco.xbjcc.constants.CourseConstants;
import com.shkco.xbjcc.domain.CourseType;
import com.shkco.xbjcc.mapper.CourseTypeMapper;
import com.shkco.xbjcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shkco.xbjcc.vo.courseTypeCrumbsVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-10-04
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {

    @Override
    @Cacheable(cacheNames = CourseConstants.COURSE_TYPE_TREE_DATA_CACHENAMES, key = CourseConstants.COURSE_TYPE_TREE_DATA_KEY)
    public List<CourseType> treeData() {
        List<CourseType> courseTypes = selectList(null);
        List<CourseType> treeData = new ArrayList<>();
        Map<Long, CourseType> collect = courseTypes.stream().collect(Collectors.toMap(CourseType::getId, courseType -> courseType));
        courseTypes.forEach(e->{
            if (e.getPid() != null && e.getPid().intValue() == 0){
                treeData.add(e);
            } else {
                CourseType courseType = collect.get(e.getPid());
                if (courseType != null){
                    courseType.getChildren().add(e);
                }
            }
        });
        return treeData;
    }

    @Override
    public List<courseTypeCrumbsVo> crumbs(Long courseTypeId) {
        CourseType courseType = selectById(courseTypeId);
        AssertUtil.isNotNull(courseType, GlobalExceptionCode.COURSE_TYPE_INEXISTENCE);
        String path = courseType.getPath();
        AssertUtil.isNotNull(path, GlobalExceptionCode.COURSE_TYPE_INEXISTENCE);
        String[] split = path.split("\\.");
        List<courseTypeCrumbsVo> crumbs = new ArrayList<>();
        for (String courseTypePath : split) {
            courseTypeCrumbsVo courseTypeCrumbsVo = new courseTypeCrumbsVo();
            CourseType ownerProductType = selectById(courseTypePath);
            courseTypeCrumbsVo.setOwnerProductType(ownerProductType);
            Wrapper<CourseType> wrapper = new EntityWrapper<>();
            wrapper.eq("pid",ownerProductType.getPid());
            List<CourseType> otherProductTypes = selectList(wrapper);
            courseTypeCrumbsVo.setOtherProductTypes(otherProductTypes);
            crumbs.add(courseTypeCrumbsVo);
        }
        return crumbs;
    }


    @Override
    @CacheEvict(cacheNames = CourseConstants.COURSE_TYPE_TREE_DATA_CACHENAMES, key = CourseConstants.COURSE_TYPE_TREE_DATA_KEY)
    public boolean insert(CourseType entity) {
        return super.insert(entity);
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.COURSE_TYPE_TREE_DATA_CACHENAMES, key = CourseConstants.COURSE_TYPE_TREE_DATA_KEY)
    public boolean updateById(CourseType entity) {
        return super.updateById(entity);
    }

    @Override
    @CacheEvict(cacheNames = CourseConstants.COURSE_TYPE_TREE_DATA_CACHENAMES, key = CourseConstants.COURSE_TYPE_TREE_DATA_KEY)
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }
}
